/**
 * @author:albert
 * @date:2016年6月3日
 */
package com.bkbw.live.battle.business;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * @author albert
 *
 */
public class HateList {
	
	public static final float NormalAttackHate = 10.0f;
	public static final float CriAttackHate = 15f;
	public static final float PursueReduce = 0.05f;
	
	public static class HateNode {
		int id;
		float hate;
		public int getId() {
			return id;
		}
		public float getHate() {
			return hate;
		}
		public void addHate(float value) {
			hate += value;
			if (hate > maxHate) {
				hate = maxHate;
			}
		}
		
		//减少仇恨，若仇恨小于0，可以移除了
		public boolean subHate(float value) {
			hate -= value;
			if (hate <= 0) {
				return true;
			}
			return false;
		}
	}
	private LinkedList<HateNode> list = new LinkedList<HateList.HateNode>();
	private HashMap<Integer, HateNode> map = new HashMap<Integer, HateNode>();
	
	private static final float maxHate = 20;
	//private HashMap<Integer, Float> hateValues;
	public void addHate(int id, float value) {
		if (map.containsKey(id)){
			map.get(id).addHate(value);
			
		}
		else {
			if (value > maxHate) {
				value = maxHate;
			}
			HateNode node = new HateNode();
			node.id = id;
			node.addHate(value);
			map.put(id, node);
			list.add(node);
		}
	}
	
	public void subHate(int id, float value) {
		if (map.containsKey(id)){
			map.get(id).subHate(value);
			
		}
	}
	
	// 随时间流失而减少，在这里才进行排序，之所以没有在add和sub时排序，是为了效率
	public void updateHateByTime(float dt) {
		Iterator<HateNode> iterator = list.iterator();
		while (iterator.hasNext()) {
			HateNode node = iterator.next();
			if (node.subHate(dt)){
				iterator.remove();
				map.remove(node.id);
			}
		}
		// 按照降序排列
		if (list.size() > 0) {
			list.sort((HateNode o1, HateNode o2) -> {
				if (o1.hate > o2.hate)
					return -1;
				else if (o1.hate == o2.hate)
					return 0;
				else 
					return 1;
			});
		}
	}
	
	public float getHateValue(int id) {
		if (map.containsKey(id)){
			return map.get(id).hate;
		}
		return 0;
	}
	
	public int getMaxHateId() {
		if (list.size() > 0) {
			return list.get(0).id;
		}
		return 0;
	}
	
	public HateNode getHateNode(int id) {
		if (map.containsKey(id)){
			return map.get(id);
		}
		return null;
	}
	
	public HateNode getMaxHateNode() {
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
}
